package com.bootdo.system.dao;

import com.bootdo.system.domain.RedBag;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface RedBagMapper {

	@Select("select * from tb_RedBag where id = #{id}")
	RedBag get(Long id);
	
	@Select("<script>" +
	"select * from tb_redbag " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"bagStatus != null and bagStatus != ''\">"+ "and bagStatus = #{bagStatus} " + "</if>" + 
	  		  "<if test=\"bagType != null and bagType != ''\">"+ "and bagType = #{bagType} " + "</if>" + 
	  		  "<if test=\"ownerId != null and ownerId != ''\">"+ "and ownerId = #{ownerId} " + "</if>" + 
	  		  "<if test=\"shareId != null and shareId != ''\">"+ "and shareId = #{shareId} " + "</if>" + 
	  		  "  and ((bagType=0 and bagMoney &lt;= #{newPoint})  or (bagType=1 and bagMoney &lt;=#{sharePoint}))" + 
	  		   " and ( (TIMESTAMPADD(minute,(select value from sys_dict where type='newBagEndTime' limit 1),endTime)>now() and bagName='新用户红包')" + 
	  		   " 		or (TIMESTAMPADD(minute,(select value from sys_dict where type='shareBagEndTime' limit 1),endTime)>now() and bagName='分享红包')" + 
	  		    ") "+
	  		  "</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<RedBag> list(Map<String,Object> map);
	
	
	@Select("<script>" +
			"select * from tb_redbag " + 
					"<where>" + 
					 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
			  		  "<if test=\"bagStatus != null and bagStatus != ''\">"+ "and bagStatus = #{bagStatus} " + "</if>" + 
			  		  "<if test=\"bagType != null and bagType != ''\">"+ "and bagType = #{bagType} " + "</if>" + 
			  		  "<if test=\"ownerId != null and ownerId != ''\">"+ "and ownerId = #{ownerId} " + "</if>" + 
			  		  "<if test=\"shareId != null and shareId != ''\">"+ "and shareId = #{shareId} " + "</if>" + 
			  		  "</where>"+ 
					" <choose>" + 
			            "<when test=\"sort != null and sort.trim() != ''\">" + 
			                "order by ${sort} ${order}" + 
			            "</when>" + 
						"<otherwise>" + 
			                "order by id desc" + 
						"</otherwise>" + 
			        "</choose>"+
					"</script>")
			List<RedBag> myBagList(Map<String,Object> map);
	
	
	
	@Select("<script>" +
	"select * from(select * from(" + 
	" select id,bagName,bagType," + 
	" case bagType when 0 then bagMoney else bagMoney*(select tr.value from sys_dict tr where tr.type='liudian' limit 1)end bagMoney "
	+ " from tb_redbag "
	+ " where ownerId = #{ownerId}" + 
	") t "
	+ " where  (t.bagType=0 and bagMoney &lt;= #{newPoint}) "
	+ " or (t.bagType=1 and bagMoney &lt;=#{sharePoint})"
	+ " order by bagMoney desc limit 1) t2"+
	"</script>")
	RedBag getMaxRedBag(Map<String,Object> map);
	
	
	@Select("<script>" +
			"select count(*) from tb_redbag " + 
					"<where>" + 
			  		  "<if test=\"bagStatus != null and bagStatus != ''\">"+ "and bagStatus = #{bagStatus} " + "</if>" + 
			  		  "<if test=\"ownerId != null and ownerId != ''\">"+ "and ownerId = #{ownerId} " + "</if>" + 
			  		  "</where>"+ 
					"</script>")
	int count(Map<String,Object> map);
	
	
	
	@Select("<script>" +
			"select count(*) from tb_redbag " + 
					"<where>" + 
			  		  "<if test=\"bagStatus != null and bagStatus != ''\">"+ "and bagStatus = #{bagStatus} " + "</if>" + 
			  		  "<if test=\"ownerId != null and ownerId != ''\">"+ "and ownerId = #{ownerId} " + "</if>" + 
			  		  "  and ((bagType=0 and bagMoney &lt;= #{newPoint})  or (bagType=1 and bagMoney &lt;=#{sharePoint}))" + 
			  		   " and ( (TIMESTAMPADD(minute,(select value from sys_dict where type='newBagEndTime' limit 1),endTime)>now() and bagName='新用户红包')" + 
			  		   " 		or (TIMESTAMPADD(minute,(select value from sys_dict where type='shareBagEndTime' limit 1),endTime)>now() and bagName='分享红包')" + 
			  		    ") "+
			  		  "</where>"+ 
					"</script>")
	int countCanUse(Map<String,Object> map);
	
	@Insert("insert into tb_redbag (`bagName`, `bagMoney`, `bagStatus`, `bagType`, `ownerId`, `createUserId`, `createTime`,`endTime`,`shareId`,`userPhone`)"
	+ "values (#{bagName}, #{bagMoney}, #{bagStatus},  #{bagType}, #{ownerId},#{createUserId}, now(), #{endTime}, #{shareId}, #{userPhone})")
	@Options(useGeneratedKeys=true)
	int save(RedBag bComments);
	
	@Update("<script>"+ 
			"update tb_redbag " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"bagName != null\">`bagName` = #{bagName}, </if>" + 
                    "<if test=\"bagMoney != null\">`bagMoney` = #{bagMoney}, </if>" + 
                    "<if test=\"bagStatus != null\">`bagStatus` = #{bagStatus}, </if>" + 
                    "<if test=\"bagType != null\">`bagType` = #{bagType}, </if>" + 
                    "<if test=\"ownerId != null\">`ownerId` = #{ownerId}, </if>" +   
                    "<if test=\"endTime != null\">`endTime` = #{endTime}, </if>" +    
                    "<if test=\"shareId != null\">`shareId` = #{shareId}, </if>" + 
                    "<if test=\"userPhone != null\">`userPhone` = #{userPhone}, </if>" + 
                    "`userPhone` = now(), " + 
          			"</set>" + 
					"where id = #{id}"+
			"</script>")
	int update(RedBag bComments);
	
	@Delete("delete from tb_redbag where id =#{id}")
	int remove(Long id);
	
	@Delete("<script>"+ 
			"delete from tb_redbag where id in " + 
					"<foreach item=\"id\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">" + 
						"#{id}" + 
					"</foreach>"+
			"</script>")
	int batchRemove(Integer[] ids);
}
